/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.manager;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class ManagerManagerBean implements ManagerManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addManager(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            ManagerEntity m = new ManagerEntity();
            m.setData(to);
            m.setDeleted(Boolean.FALSE);
            this.em.persist(m);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ManagerManagerException("Cannot add manager", e);
        }
    }

    public void editManager(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            ManagerEntity m = this.em.find(ManagerEntity.class, to.getId());
            m.setData(to);
            this.em.merge(m);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot edit manager", e);
        }
    }

    public void editManagerDetails(ManagerDetailedTO to) throws ManagerManagerException {
        try {
            ManagerEntity m = this.em.find(ManagerEntity.class, to.getId());
            m.setDetails(to);
            this.em.merge(m);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot edit manager", e);
        }
    }

    public void deleteManager(Long id) throws ManagerManagerException {
        try {
            ManagerEntity m = this.em.find(ManagerEntity.class, id);
            m.setDeleted(Boolean.TRUE);
            this.em.merge(m);
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot delete manager", e);
        }
    }

    public ManagerDetailedTO getManager(Long id) throws ManagerManagerException {
        try {
            ManagerEntity m = em.find(ManagerEntity.class, id);
            return m.getDetailedData();
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get manager by ID " + id.longValue(), e);
        }
    }

    public List<ManagerTO> getAllManagers() throws ManagerManagerException {
        try {
            List<ManagerTO> results = new ArrayList<ManagerTO>();
            Query allManagersQuery = em.createNamedQuery("findAllManagers");
            List dbResults = allManagersQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                ManagerEntity d = (ManagerEntity) dbResults.get(i);
                results.add(d.getData());
            }
            return results;
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get all managers", e);
        }
    }

    public List<ManagerDetailedTO> getAllDetailedManagers() throws ManagerManagerException {
        try {
            List<ManagerDetailedTO> results = new ArrayList<ManagerDetailedTO>();
            Query allManagersQuery = em.createNamedQuery("findAllManagers");
            List dbResults = allManagersQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                ManagerEntity m = (ManagerEntity) dbResults.get(i);
                results.add(m.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot get all detailed managers", e);
        }
    }

    public boolean isUsernameAvailable(String username) throws ManagerManagerException {
        try {
            Query findDriverByUsernameQuery = this.em.createNamedQuery("findManagerByUsername");
            findDriverByUsernameQuery.setParameter("username", username);

            List managers = findDriverByUsernameQuery.getResultList();
            if (managers != null && managers.size() != 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot check if the username exist", e);
        }
    }

    /**
     * Authenticate manager using username and password
     * @param username
     * @param password
     * @return Manager ID
     * @throws au.edu.swin.swinvehicle.driver.DriverManagerException
     */
    public Long authenticate(String username, String password) throws ManagerManagerException {
        try {
            Query findManagerByUsernameQuery = this.em.createNamedQuery("findManagerByUsername");
            findManagerByUsernameQuery.setParameter("username", username);

            List managers = findManagerByUsernameQuery.getResultList();
            if (managers != null && managers.size() > 0) {
                ManagerEntity manager = (ManagerEntity) managers.get(0);

                if (manager.getPassword().equals(password)) {
                    return manager.getId();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new ManagerManagerException("Cannot authenticate manager with username " + username, e);
        }
    }
}
